//
//  NetworkHelper.swift
//  SwiftWB
//
//  Created by 远成物流 on 2017/6/27.
//
//

import AFNetworking

enum RequestType : Int{
    case GET = 0
    case POST = 1
}

class NetworkHelper: AFHTTPSessionManager {
    
    static let shareInstance : NetworkHelper = {
       
        let helper = NetworkHelper()
        helper.responseSerializer.acceptableContentTypes?.insert("text/html")
        helper.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return helper
    }()
}

/// Mark - GET & POST
extension NetworkHelper{
    
    func request(methed:RequestType , url:String , parameters:[String:Any] , finished:@escaping ((_ result:AnyObject? , _ error:AnyObject?) -> ())) {
        
        let successBlock = { (task:URLSessionDataTask, result:Any?) in
            
            Log(result)
            
            finished(result as AnyObject?, nil)
        }
        
        let failureBlock = { (task:URLSessionDataTask?, error:Error) in
            
            Log(error.localizedDescription)
            
            finished(nil, error as AnyObject?)
        }
        
        if methed == .GET {
            get(url, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }else{
            post(url, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
        
    }
    
}

extension NetworkHelper{
    
    //获取授权token
    func getAccessToken(code:String , finished: @escaping ((_ result:[String:AnyObject]? , _ error:Error?)->())) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = ["client_id" : Appkey, "client_secret" : Appsecret, "grant_type" : "authorization_code", "redirect_uri" : AppRedirectURI, "code" : code]
        
        request(methed: .POST, url: urlString, parameters: parameters) { (result, error) in
            finished(result as? [String:AnyObject] , error as? Error)
        }
    }
    
    //获取用户信息
    func getUserInfo(accessToken:String , uid:String , finished:@escaping( (_ result:[String:AnyObject]? , _ error:Error?)->() )) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parameters = ["access_token" : accessToken, "uid" : uid]
        
        request(methed: .GET, url: urlString, parameters: parameters) { (result, error) in
            
            finished(result as? [String:AnyObject], error as? Error)
        }
    }
    
    //获取首页微博数据
    func loadStatues(sinceId:Int , maxId:Int , finished:@escaping (_ result:[[String:AnyObject]]? , _ error:Error?)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parameters = ["access_token":(UserAccoutViewModel.shareInstance.account?.access_token)!,"since_id": "\(sinceId)","max_id":"\(maxId)"]
        
        request(methed: .GET, url: urlString, parameters: parameters) { (result, error) in
            
            //判断是否有返回数据
            guard let json = result as? [String:AnyObject] else{
                finished(nil, error as? Error)
                return
            }
            
            finished(json["statuses"] as? [[String:AnyObject]], error as? Error)
        }
    }
}
